<template>
	<view class="content">
		<view class="product-item mb-4">
			<view class="d-flex align-center pb-4 mb-2 u-border-bottom">
				<text class="font-bold mr-4">套餐名称</text>
				<text>{{products.in_name}}</text>
			</view>
			<!-- <view class="d-flex align-center">
				<text class="font-bold  mr-4">是否启用</text>
				<view>
					<radio-group @change="radioChange" class="d-flex align-center">
						<label class="mr-4 d-flex align-center">
							<radio value="1" :checked="radiovalue == 1" /><text>启用</text>
						</label>
						<label class="d-flex align-center">
							<radio value="2" :checked="radiovalue == 2" /><text>禁用</text>
						</label>
					</radio-group>
				</view>
			</view> -->
		</view>
		<!-- 上传主图 -->
		<view class="d-flex align-center ">
			<text class="" style="color: #f00;">*</text>
			<text>上传图片</text>
		</view>
		<view class="product-item mt-4" @click="chooseFn" v-if="zhudata.length==0">
			<view class="d-flex justify-space-between">
				<text>请上传一张主图</text>
				<text>{{zhulength}}/1</text>
			</view>
			<view class=" uploadbox mt-2">
				<text class="font-24">+</text>
				<text>上传图片</text>
			</view>
		</view>
		<view class="mt-2 mb-2" v-else>
			<view class="images-box" v-for="(item , index) in zhudata" :key="index">
				<image style="width: 200rpx;height: 200rpx;" :src="item" mode=""></image>
				<text class="close" @click="deleteZhuimg(index)">x</text>
			</view>
		</view>
		<!-- 详情图 -->
		<view class="d-flex align-center ">
			<text class="" style="color: #f00;">*</text>
			<text>上传详情图</text>
		</view>
		<view class="product-item mt-4" @click="chooseDetailFn" v-if="detailimag.length<5">
			<view class="d-flex justify-space-between">
				<text>最多选择五张</text>
				<text>{{detaillength}}/5</text>
			</view>
			<view class=" uploadbox mt-2">
				<text class="font-24">+</text>
				<text>上传图片</text>
			</view>
		</view>
		<view class="mt-2 mb-2 d-flex align-center">
			<view class="images-box mr-2" v-for="(item , index) in detailimag" :key="index+'bbb'">
				<image style="width: 200rpx;height: 200rpx;" :src="item" mode=""></image>
				<text class="close" @click="deletedetailimg(index)">x</text>
			</view>
		</view>
		<!-- 确定按钮 -->
		<view class="submitbtn">
			<button  type="primary" @click="submitFn">确定</button>
		</view>
		<view class="introduce">
			<text>介绍:</text>
			<text>一、主图:店铺列表页缩略图</text>
			<text>二、详情图:商品页面的所有介绍</text>
		</view>
	</view>
</template>

<script>
	import {
		getToken
	} from '@/common/auth';
	export default {
		data(){
			return {
				radiovalue:'1',
				zhulength:0,//主图张数
				zhudata:[],//主图数据
				zhudatas:[],//主图数据
				detaillength:0,//详情图张数
				detailimag:[],//详情图数据
				detailimags:[],//详情图数据
				goodsId:0,
				products:{}
			}
		},
		onShow() {
			let nowApp = getApp();
			// console.log(nowApp)
			if (true === uni.$uv.getProperty(nowApp, 'globalData.reInit')) {
				getApp().globalData.reInit = false;
				console.log('重新执行init');
				this.goods();
			}
		},
		onReady() {
		
		},
		onLoad(options) {
			// console.log(options,44444444444);
			if (options.shopId) {
				// 保存当前shopId
				uni.setStorage({
					key: 'shop_id',
					data: options.shopId
				});
				uni.$my.shopId = options.shopId;
			}
			let aShopId = uni.getStorageSync('shop_id');
			if ((aShopId)) {
				uni.$my.shopId = aShopId;
			}
			// console.log(uni.$my.shopId,44444444);
			this.goodsId = options.id;
			this.serverHost = uni.$my.serverHost;
			this.goods();
		},
		methods:{
			isEmpty(item) {
				return uni.$uv.test.empty(item)
			},
			submitFn(){
				// uni.showToast({
				// 	title:'提交成功'
				// })
				if(this.zhudata.length <= 0 && this.detailimag.length <=0){
					uni.showToast({
						title:'请上传图片'
					})
					return false;
				}
				uni.$uv.http.post('/goods/goodsEditCommission',{id:this.goodsId,zhuData:this.zhudatas,detailimag:this.detailimags}).then(res => {
					// console.log(res);
					let goodsName = this.products.in_name;
					uni.showToast({
						title:goodsName+res.msg
					})
					getApp().globalData.reInit = true;
					setTimeout(()=>{
						uni.switchTab({
							url: "/pages/index/index",
							success: () => {
								// uni.removeStorage({
								// 	key: '_login_back_page',
								// 	success: function(res) {
								// 		console.log('success');
								// 	}
								// });
							}
						});
					},300);
					// this.bannerlist = res.data.swiper;
					// this.products = this.makeServerPicUrl(res.data || '', 'main_pic')
				}).catch(e => {
					console.log(e)
				})
			},
			radioChange(e){
				this.radiovalue = e.detail.value
			},
			// 删除主图
			deleteZhuimg(index){
				this.zhudata.splice(index , 1)
			},
			// 图片上传
			chooseFn(){
				const nowToken = getToken();
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: '/dev-api/uploadPic', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header:{
								"Authorization":'Bearer ' + nowToken
							},
							formData: {
								'user': 'test'  // 其他要传的参数
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data);
								let url = this.serverHost + res.fileName;
								this.zhulength = 1;
								this.zhudata= [url];
								this.zhudatas= [res.fileName];
							}
						});
					}
				});
			},
			// 删除详情图
			deletedetailimg(index){
				this.detailimag.splice(index , 1)
			},
			chooseDetailFn(){
				const nowToken = getToken();
				uni.chooseImage({
					count: 5, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: '/dev-api/uploadPic', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header:{
								"Authorization":'Bearer ' + nowToken
							},
							formData: {
								'user': 'test'  // 其他要传的参数
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data);
								let url = this.serverHost + res.fileName;
								
								
								this.detailimag.push(url);
								this.detailimags.push(res.fileName);
								this.detaillength = this.detailimag.length;
							}
						});
					}
				});
			},
			goods() {
				uni.$uv.http.get('/goods/detail',{
					params: {
						id: this.goodsId
					}
				}).then(res => {
					console.log(res);
					// this.bannerlist = res.data.swiper;
					this.products = this.makeServerPicUrl(res.data || '', 'main_pic')
				}).catch(e => {
					console.log(e)
				})
			},
			makeServerPicUrl(rowsArray, picField) {
				if (!this.isEmpty(rowsArray)) {
					// console.log(1111111);
					if (uni.$uv.test.array(rowsArray)) {
						for (let i = 0; i < rowsArray.length; i++) {
							// console.log(this.serverHost,2222222222);
							rowsArray[i][picField] = this.serverHost + rowsArray[i][picField]
						}
					} else if (uni.$uv.test.object(rowsArray)) {
						// console.log(this.serverHost,333333333333333);
						rowsArray[picField] = this.serverHost + rowsArray[picField]
					}
				}
				return rowsArray
			},
		}
	}
</script>

<style scoped lang="scss">
	.content{
		padding: 20rpx;
		padding-bottom: 100rpx;
	}
	.product-item{
		background: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
	}
	.uploadbox{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f4f5f6;
		border-radius: 12rpx;
		width: 200rpx;
		height: 200rpx;
	}
	.submitbtn{
		margin: 24rpx auto;
	}
	.introduce{
		display: flex;
		color: #a0a1a7;
		flex-direction: column;
	}
	.images-box{
		width: 200rpx;
		height: 200rpx;
		position: relative;
		image:{
			width: 200rpx;
			height: 200rpx;
		}
		.close{
			position: absolute;
			top: 10rpx;
			right: 10rpx;
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			color: #fff;
			text-align: center;
			line-height: 50rpx;
			background-color:#f00;
		}
	}
</style>